package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 物资采购合同流程调用SAP采购订单审批回传接口（MM014)
 * @ModifyLog:
 **/
public class WeaWzcghtlcBackAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaWzcghtlcBackAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String ywxtddh = Util.null2String(rs.getString("ywxtddh"));
            String pzbs = Util.null2String(rs.getString("pzbs"));
            String htly = Util.null2String(rs.getString("htly"));
            if ("1".equals(htly)) {
                JSONObject dataJson = new JSONObject();
                JSONObject dataheaderJson = new JSONObject();
                dataheaderJson.put("SERIAL", requestid);
                dataheaderJson.put("OANUM", requestid);
                dataheaderJson.put("BSART", "");
                dataheaderJson.put("EBELN", ywxtddh);
                dataheaderJson.put("FRGKE", pzbs);
                dataJson.put("DATAHEADER", dataheaderJson);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaWzcghtlcBackAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaWzcghtlcBackAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("MM014", dataJson, dataMap);
                writeLog(">>>>>WeaWzcghtlcBackAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if (!"S".equalsIgnoreCase(msgType)) {
                        message += outputJson.getString("MSG_TEXT");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaWzcghtlcBackAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}